package com.taskinception.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;

public class Alert {

    public static final int POSITIVE = AlertDialog.BUTTON_POSITIVE;
    public static final int NEUTRAL = AlertDialog.BUTTON_NEUTRAL;
    public static final int NEGATIVE = AlertDialog.BUTTON_NEGATIVE;

    private AlertDialog mDialog;
    private Context mCtx;

    public Alert(Context ctx) {
	mCtx = ctx;
	mDialog = new AlertDialog.Builder(ctx).create();
    }

    public Alert(Context ctx, int title, int message) {
	this(ctx);
	mDialog.setTitle(title);
	mDialog.setMessage(ctx.getString(message));
    }

    public void show() {
	mDialog.show();
    }

    public void dismiss() {
	mDialog.dismiss();
    }

    public void setTitle(int title) {
	mDialog.setTitle(title);
    }

    public void setMessage(int message) {
	mDialog.setMessage(mCtx.getString(message));
    }

    public void setButton(int whichButton, CharSequence label,
	    OnClickListener listener) {
	mDialog.setButton(whichButton, label, listener);
    }

    /**
     * Create and show an {@code AlertDialog} with one button and add an
     * {@code OnClickListener} to that button. The {@code OnClickListener} gives
     * control over the actions after clicking the "OK" button.
     * 
     * @param ctx
     *            Context to run the dialog in. Best practice would be
     *            {@code CurrentClass.this}
     * @param title
     *            Resource id of the {@code String} to show as title
     * @param message
     *            Resource id of the {@code String} to show as message
     * @param listener
     *            {@code OnClickListener} to listen to the click event
     */
    public static void message(Context ctx, int title, int message,
	    OnClickListener listener) {
	message(ctx, ctx.getString(title), ctx.getString(message), listener);
    }

    public static void message(Context ctx, String title, String message,
	    OnClickListener listener) {
	AlertDialog dialog = new AlertDialog.Builder(ctx).create();
	dialog.setTitle(title);
	dialog.setMessage(message);
	dialog.setButton(NEUTRAL, ctx.getString(android.R.string.ok), listener);
	dialog.show();
    }

    /**
     * Create and show an {@link AlertDialog} with one button. The dialog closes
     * when button "OK" has been clicked
     * 
     * @param ctx
     *            Context to run the dialog in. Best practice would be
     *            {@code CurrentClass.this}
     * @param title
     *            Resource id of the {@code String} to show as title
     * @param message
     *            Resource id of the {@code String} to show as message
     */
    public static void message(Context ctx, int title, int message) {
	message(ctx, title, message, null);
    }

    /**
     * Create and show an {@link AlertDialog} with two buttons and add
     * {@link OnClickListener}s to listener to their respective click event
     * 
     * @param ctx
     *            {@link Context} to run the dialog in. Best practice would be
     *            {@code CurrentClass.this}
     * @param title
     *            Resource id of the {@code String} to show as title
     * @param message
     *            Resource id of the {@code String} to show as message
     * @param positiveListener
     *            {@code OnClickListener} for the "YES" button
     * @param negativeListener
     *            {@code OnClickListener} for the "NO" button
     */
    public static void confirm(Context ctx, int title, int message,
	    OnClickListener positiveListener, OnClickListener negativeListener) {
	AlertDialog dialog = new AlertDialog.Builder(ctx).create();
	dialog.setTitle(title);
	dialog.setMessage(ctx.getResources().getString(message));
	dialog.setCanceledOnTouchOutside(false);
	dialog.setCancelable(false);

	dialog.setButton(POSITIVE, ctx.getString(android.R.string.ok),
		positiveListener);
	dialog.setButton(NEGATIVE, ctx.getString(android.R.string.no),
		negativeListener);
	dialog.show();
    }
    
    public static void confirm(Context ctx, String title, String message,
	    OnClickListener positiveListener, OnClickListener negativeListener) {
	AlertDialog dialog = new AlertDialog.Builder(ctx).create();
	dialog.setTitle(title);
	dialog.setMessage(message);
	dialog.setCanceledOnTouchOutside(false);
	dialog.setCancelable(false);

	dialog.setButton(POSITIVE,
		ctx.getResources().getString(android.R.string.ok),
		positiveListener);
	dialog.setButton(NEGATIVE,
		ctx.getResources().getString(android.R.string.no),
		negativeListener);
	dialog.show();
    }
    
    /**
     * Create and show an {@link AlertDialog} with two buttons and add
     * {@link OnClickListener}s to listener to their respective click event
     * 
     * @param ctx
     *            {@link Context} to run the dialog in. Best practice would be
     *            {@code CurrentClass.this}
     * @param title
     *            Resource id of the {@code String} to show as title
     * @param message
     *            Resource id of the {@code String} to show as message
     * @param messageDetail
     *            String which will be displayed after message
     * @param positiveListener
     *            {@code OnClickListener} for the "YES" button
     * @param negativeListener
     *            {@code OnClickListener} for the "NO" button
     */
    public static void confirm(Context ctx, int title, int message, String messageDetail,
	    OnClickListener positiveListener, OnClickListener negativeListener) {
	AlertDialog dialog = new AlertDialog.Builder(ctx).create();
	dialog.setTitle(title);
	dialog.setMessage(ctx.getResources().getString(message) + ": " + messageDetail);
	dialog.setCanceledOnTouchOutside(false);
	dialog.setCancelable(false);

	dialog.setButton(POSITIVE,
		ctx.getResources().getString(android.R.string.ok),
		positiveListener);
	dialog.setButton(NEGATIVE,
		ctx.getResources().getString(android.R.string.no),
		negativeListener);
	dialog.show();
    }
}
